package net.wanho.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import net.wanho.po.Emp;
import net.wanho.mapper.EmpMapper;
import net.wanho.po.User;
import net.wanho.service.EmpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author 汤小洋
 * @since 2023-09-27
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

   /**
    *  员工表 按查询分页查询
    */
    @Override
    public IPage<Emp> findPage(int pageNum, int pageSize,Emp emp) {
        //验证
        if(pageNum<1)
            pageNum=1;
        if(pageSize<1)
            pageSize=10;
        if(ObjectUtils.isEmpty(emp))
            emp = new Emp();

        // 查询条件
        QueryWrapper<Emp> wrapper = getEmpQueryWrapper(emp);
        IPage<Emp> page = new Page<>(pageNum, pageSize);
        page = this.page(page, wrapper);
        return page;
    }

    @Override
    public List<Emp> findAll() {
        return this.baseMapper.selectAll();
    }

    @Override
    public IPage<Emp> findByPage(IPage<Emp> page, Wrapper<Emp> queryWrapper) {
        return this.baseMapper.selectByPage(page,queryWrapper);
    }

    /**
    *  员工表 查询条件
    */
     private QueryWrapper<Emp> getEmpQueryWrapper(Emp emp) {
        QueryWrapper<Emp> wrapper = new QueryWrapper<Emp>();
        // 编号
        wrapper.lambda().eq(!ObjectUtils.isEmpty(emp.getId()),Emp::getId, emp.getId());
        // 姓名
        wrapper.lambda().eq(!ObjectUtils.isEmpty(emp.getName()),Emp::getName, emp.getName());
        // 工资
        wrapper.lambda().eq(!ObjectUtils.isEmpty(emp.getSalary()),Emp::getSalary, emp.getSalary());
        // 部门编号
        wrapper.lambda().eq(!ObjectUtils.isEmpty(emp.getDeptId()),Emp::getDeptId, emp.getDeptId());
        return wrapper;
    }
}
